DO NOT COPY OR SHARE - A3Tech / NCTI course material for enrolled students only
A3Tech logo

Introduction to Python Programming · A3Tech

Module 2

Today you will

Store values in variables, ask the user questions, and do the shop's maths, including 16% VAT.

  1. Create and change variables, and know the difference between text (str), whole numbers (int) and decimals (float).
  2. Ask the user a question with input() and convert the answer with int() or float().
  3. Calculate totals and VAT with + - * /.
  4. Show money neatly with f-strings, like f"EC${total:.2f}".

1. Variables

A variable is a name that stores a value. Run this, then change the price and run it again.

01_variables.py
# Module 2 - Variables: names that store values
shop_name = "Mama J's Bakery"     # a string (text)
price = 5.00                      # a float (a number with a decimal point)
qty = 3                           # an int (a whole number)

total = price * qty
print(shop_name)
print("3 patties cost EC$", total)

price = 5.50                      # variables can change
print("New price:", price)
var_types.py
name = "your name"
age = "45"
age2 = 45
is_male = True
bdy_temp = 98.5

print(type(bdy_temp))
Output
<class 'float'>

WRONG VARIABLE TYPE

var_type_error.py
name = "your name"
age = "45"
is_male = True
bdy_temp = 98.986

print(age + 1) #Cannot add an integer to a string for a math calc
Output
Traceback (most recent call last):
  File "<string>", line 6, in <module>
TypeError: can only concatenate str (not "int") to str
converted_var.py
name = "your name"
age = "45"
is_male = True
bdy_temp = 98.986

print(int(age) + 1) # the string was converted to a number before the calculation was done
Output
46

2. The shop calculator

This asks how many patties, then works out the subtotal, VAT and total. Run it and type a number.

02_calculator.py
# shop.py - Module 2: the price calculator
price = 5.00
qty = int(input("How many beef patties? "))   # input() gives text; int() turns it into a number

subtotal = price * qty
vat = subtotal * 0.16                          # 16% VAT
total = subtotal + vat

print(f"Subtotal: EC${subtotal:.2f}")          # :.2f shows 2 decimal places
print(f"VAT 16%:  EC${vat:.2f}")
print(f"Total:    EC${total:.2f}")

2.1. input() and f-string

input.py
# using the input() built-in function

name = input("What is your name? ")   # ask the user for input (always comes back as text)
age = int(input("How old are you? "))  # input runs first, then int() turns the answer into a number
gender = input("Are you male or female? (male/female): ") #give the user accepted answers to choose from
bdy_temp = 98.986454

print("Hi, I am", name, "- I am", age, "years old and I identify as", gender)
print("I do not have covid, my temp is only", round(bdy_temp, 2))
Output
What is your name? alex
How old are you? 45
Are you male or female? (male/female): male
Hi, I am alex - I am 45 years old and I identify as male
I do not have covid, my temp is only 98.99

f-string - an f-string is text with gaps you can fill in. Put an f before the opening quote, then put any variable or sum inside { }, and Python swaps it for the value.

fstring.py
name = "Mama J"
price = 5.00
quantity = 2

# put an f before the first " - then put the variable you want inside { }
# and Python swaps the { } for that variable's value
print(f"Welcome to {name}'s Bakery")

# you can do calculations inside the { } too
# :.2f means "show 2 decimal places" - perfect for money
print(f"{quantity} patties cost EC${price * quantity:.2f}")
Output
Welcome to Mama J's Bakery
2 patties cost EC$10.00

ADDITIONAL - STREAMLIT APP - SNEAK PEEK 2

The same idea as input() and f-strings, but in the browser. Instead of typing answers in the terminal, the user types into boxes on a web page.

shop_input_app.py
# shop_input_app.py - input boxes and f-strings in a Streamlit app
# Run it with:  streamlit run shop_input_app.py

import streamlit as st

st.title("Mama J's Bakery")

name = st.text_input("What is your name?")                 # a text box: gives back a str
qty = st.number_input("How many patties?", min_value=1, value=2, step=1)   # a number box: gives back an int
price = 5.00                                               # float

total = price * qty
st.write(f"Hi {name}! {qty} patties cost EC${total:.2f}")
Output
See the browser: type a name, pick a number, e.g.
Hi Alex! 3 patties cost EC$15.00

Words to know

WordMeaning
VariableA name that stores a value: price = 5.00
strText: "Beef Patty"
intA whole number: 3
floatA number with a decimal point: 5.00
input()Asks the user a question and returns their answer as text
f-stringText with values inside: f"Total: {total}"

Bug hunt

Run this and type 3. It crashes with a TypeError. Read the last line, then fix it.

03_input_bug.py
# BUG HUNT - why does this crash?
# Run it, type 3, and read the last line of the error.
qty = input("How many patties? ")
total = qty * 5.00
print("Total:", total)
Hints (open only if stuck)
  1. What kind of value does input() give back?
  2. How do you turn text into a whole number?

Practice

Homework 2 (5%): change_calculator.py

A program a cashier could use. It must:

Submit your .py file and a screenshot of it running, before the next module.

MarkMeans
5Runs and has everything
3Runs with parts missing, or has errors
1Started
0Not submitted

Quiz 2

Check yourself here, then take the graded quiz in the quiz section of robotics.a3tech.uk (two tries).

1. What does this print?
price = 5
qty = 3
print(price * qty)
2. What does input() always give back?
3. What does this print?
total = 17.4
print(f"EC${total:.2f}")
4. Which line correctly gets a whole number from the user?
5. What is the value of x at the end?
x = 10
x = x + 5